package com.linkgie.galaxyframework.metric;

import com.linkgie.galaxyframework.utils.Assert;

public class MetricDomains {

    private String code;

    private String name;


    private MetricDomains() {
    }

    public MetricDomains name(String name){
        this.name = name;
        return this;
    }

    public MetricDomain create(){
        return new MetricDomainEntry(this.code, this.name == null ? this.code : this.name);
    }

    public static MetricDomains of(String code) {
        Assert.notBlank(code, "领域编码为空");
        Assert.isTrue(code.length() <= 64, "领域编码长度大于64");
        Assert.isTrue(CodeUtils.CODE_PATTERN.matcher(code).matches(), "领域编码违反规则：只包含数字、字母、下划线、减号");

        MetricDomains domain = new MetricDomains();
        domain.code = code;
        return domain;
    }

    private static class MetricDomainEntry implements MetricDomain {
        private String code;

        private String name;

        MetricDomainEntry(String code, String name){
            this.code = code;
            this.name = name;
        }

        /**
         * @return the code
         */
        public String getCode() {
            return code;
        }

        /**
         * @return the name
         */
        public String getName() {
            return name;
        }

    }

}
